﻿using System;
using System.ComponentModel;
using System.Xml;
using Moonlite.Data.EditorInterop.Internal;

namespace Moonlite.Data.EditorInterop
{
    [DefaultProperty("Id")]
    [DisplayName("Name")]
    public class ObjectPoint : ObjectUnit
    {
        public const int PointOwnerPlayer = 2000;

        private string name;
        private float[] color;

        public string Name
        {
            get { return name; }
        }

        public float[] Color
        {
            get { return color; }
        }

        internal static new IDeserializedObject Parse(XmlNode node)
        {
            int id = 0;
            float[] position = null;
            float rotation = 0f;
            float[] scale = null;
            string type = "";
            string name = "";
            float[] color = null;
            foreach (XmlAttribute attribute in node.Attributes)
            {
                switch (attribute.Name.ToLower())
                {
                    case "id":
                        id = int.Parse(attribute.Value);
                        break;
                    case "position":
                        position = FloatArrayUtils.ConvertStringToFloatArray(attribute.Value);
                        break;
                    case "rotation":
                        rotation = float.Parse(attribute.Value);
                        break;
                    case "scale":
                        scale = FloatArrayUtils.ConvertStringToFloatArray(attribute.Value);
                        break;
                    case "type":
                        type = attribute.Value;
                        break;
                    case "name":
                        name = attribute.Value;
                        break;
                    case "color":
                        color = FloatArrayUtils.ConvertStringToFloatArray(attribute.Value);
                        break;
                }
            }

            IDeserializedObject @object = new ObjectPoint(id, position, rotation, scale, type, name, color);
            GenericObjectNodeParser.Parse(node, @object);
            return @object;
        }

        internal ObjectPoint(int id, float[] position, float rotation, float[] scale, string type, string name, float[] color)
            : base(id, position, rotation, scale, type, PointOwnerPlayer)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            if (color == null || color.Length == 0)
            {
                throw new ArgumentNullException("color");
            }

            this.name = name;
            this.color = color;
        }
    }
}
